package dao.impl;

import dao.ITopicDao;
import entity.Topic;
import entity.Users;
import until.DButil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TopicDaoImpl extends DButil implements ITopicDao {

    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;

    @Override
    public int getCount() {
        conn=getCon();
        int count=0;
        try {
            String sql="select count(1) from tb_topic";
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
            if (rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clsAll(conn,pst,rs);
        }

        return count;
    }
    @Override
    public List getAll(int pageSize,int pageNum) {
        List list=new ArrayList();
        conn=getCon();
        try {

            String sql="select t.*,u.col_uname from tb_topic t,tb_user u  where t.col_fdel=1 and t.col_uid=u.col_uid order by t.col_ftime desc limit ?,? ";
            pst=conn.prepareStatement(sql);
            pst.setInt(1, pageSize*(pageNum-1));
            pst.setInt(2,pageSize);
            rs=pst.executeQuery();
            while (rs.next()){
                Topic t=new Topic();
                t.setFid(rs.getInt(1));
                t.setFtitle(rs.getString(2));
                t.setFcontext(rs.getString(3));
                t.setFpic(rs.getString(4));
                t.setFtime(rs.getDate(5));
                t.setUid(new Users(rs.getInt(6)));
                t.setFdel(rs.getInt(7));
                t.setUid(new Users(rs.getString(8)));
                list.add(t);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clsAll(conn,pst,rs);
        }
        return list;
    }

    @Override
    public int addPublish(Topic topic) {
        conn=getCon();
        int len=0;
        try {
            String sql="insert into tb_topic" +
                    "(col_ftitle,col_fcontext,col_fpic,col_ftime,col_uid,col_fdel) " +
                    "values(?,?,?,now(),?,1)";
            pst=conn.prepareStatement(sql);
            pst.setString(1,topic.getFtitle());
            pst.setString(2,topic.getFcontext());
            pst.setString(3,topic.getFpic());
            pst.setInt(4,topic.getUid().getUid());
            len=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clsAll(conn,pst,null);
        }
        return len;
    }

    @Override
    public int updPublish(Topic topic,int fid) {
        conn=getCon();
        int len=0;
        try {
            String sql="update tb_topic set col_ftitle=?,col_fcontext=?,col_fpic=? where col_fid=? and col_fdel=1";
            pst=conn.prepareStatement(sql);
            pst.setString(1,topic.getFtitle());
            pst.setString(2,topic.getFcontext());
            pst.setString(3,topic.getFpic());
            pst.setInt(4,fid);
            len=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clsAll(conn,pst,null);
        }

        return len;
    }

    @Override
    public Topic getUid(int tid) {
       Topic t=null;
        conn=getCon();
        try {

            String sql="select t.*,u.col_uname from tb_topic t,tb_user u  where t.col_fdel=1 and t.col_uid=u.col_uid and col_fid=?  ";
            pst=conn.prepareStatement(sql);
            pst.setInt(1,tid);
            rs=pst.executeQuery();
            while (rs.next()){
                t=new Topic();
                t.setFid(rs.getInt(1));
                t.setFtitle(rs.getString(2));
                t.setFcontext(rs.getString(3));
                t.setFpic(rs.getString(4));
                t.setFtime(rs.getDate(5));
                t.setUid(new Users(rs.getInt(6)));
                t.setFdel(rs.getInt(7));
                t.setUid(new Users(rs.getString(8)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            clsAll(conn,pst,rs);
        }
        return t ;
    }


}
